package com.subaiwang.controller;

import com.alibaba.fastjson.JSON;
import com.subaiwang.dao.official.OfficialWebsiteMapper;
import com.subaiwang.pojo.OfficialContent;
import com.subaiwang.pojo.OfficialWebsite;
import com.subaiwang.pojo.Pages;
import com.subaiwang.service.officialWebsite.OfficialWebsiteService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/official")
public class OfficialController {

    @Resource
    private OfficialWebsiteService officialWebsiteService;

    /**
     * 塑胶管理  --塑胶类表
     * @return
     */

    @RequestMapping("/Products_List.html")
    public String products_List(HttpSession session){
        //设置一个页码
        int pageNo=1;
        Pages pages=new Pages();
        pages.setPageNo(pageNo);
        int count=officialWebsiteService.getCount(null,null);
        pages.setPageCount(count);
       officialWebsiteService.getPageList(null,null,pages);

        session.setAttribute("count",count);
        session.setAttribute("list",pages);
        return "Products_List";
    }

    /**
     *  塑胶管理  --塑胶类表  分页显示
     * @param session
     *
     * @param pageNo
     * @return
     */

   @RequestMapping(value = "/productListajax",method = RequestMethod.POST)
    @ResponseBody
    public Object doPost(HttpSession session,
                        @RequestParam(value = "typeWebsite",required = false) String typeWebsite,
                        @RequestParam(value = "name",required = false) String name,
                        @RequestParam(value = "pageNo",required = false,defaultValue = "1") int pageNo){
        int pageNoStr=pageNo;
        int pageNo2=1;
        if (pageNoStr==0) {
            pageNo2=1;
        } else {
            pageNo2=pageNoStr;
        }

        Pages pages = new Pages();
        pages.setPageNo(pageNo2);
       int count=officialWebsiteService.getCount(typeWebsite,name);
       if (typeWebsite.equals("0")){
           count=officialWebsiteService.getCount(null,name);
           officialWebsiteService.getPageList(null,name,pages);
       }else{
           count=officialWebsiteService.getCount(typeWebsite,name);
           officialWebsiteService.getPageList(typeWebsite,name,pages);
       }
       pages.setPageCount(count);



        session.setAttribute("count",count);
        session.removeAttribute("list");
        session.setAttribute("list",pages);
        String jsonarray = JSON.toJSONString(pages);
        return jsonarray;
    }
    /**
     * 塑胶管理  --详细信息管理
     * @return
     */
    @RequestMapping("/Brand_Manage.html")
    public String brand_Manage(HttpSession session)
    {
        //设置一个页码
        int pageNo=1;
        Pages pages=new Pages();
        pages.setPageNo(pageNo);
        officialWebsiteService.getOfficialContent(null,pages);
        List<OfficialWebsite> list2=officialWebsiteService.getOfficialWebsite2();
        int count=officialWebsiteService.getCount2(null);
        session.setAttribute("list2",list2);
        session.setAttribute("count",count);
        session.setAttribute("list",pages);
        return "Brand_Manage";
    }

    /**
     * 塑胶管理  --详细信息管理  分页显示
     * @return
     */
    @RequestMapping(value = "/BrandManageajax",method = RequestMethod.POST)
    @ResponseBody
    public Object doPost2(HttpSession session,
                         @RequestParam(value = "content",required = false) String content,
                         @RequestParam(value = "pageNo",required = false,defaultValue = "1") int pageNo){
        int pageNoStr=pageNo;
        int pageNo2=1;
        if (pageNoStr==0) {
            pageNo2=1;
        } else {
            pageNo2=pageNoStr;
        }

        Pages pages = new Pages();
        pages.setPageNo(pageNo2);
        int count=officialWebsiteService.getCount2(content);
        officialWebsiteService.getOfficialContent(content,pages);
        pages.setPageCount(count);

        session.setAttribute("count",count);
        session.removeAttribute("list");
        session.setAttribute("list",pages);
        String jsonarray = JSON.toJSONString(pages);
        return jsonarray;
    }

    /**
     * 详细信息管理   --跳到添加数据页面
     * @return
     */
    @RequestMapping("/Add_Brand_Manage.html")
    public String Add_Brand_Manage(Model model)
    {
        model.addAttribute("websiteList",officialWebsiteService.getOfficialWebsite2());
        return "Add_Brand_Manage";

    }
    /**
     * 详细信息管理   --添加数据
     * @return
     */
    @RequestMapping(value = "/Add_Brand_Manage.html",method = RequestMethod.POST)
    public String doAdd_Brand_Manage( @RequestParam(value = "content",required = false) String content,
                                      @RequestParam(value = "contentlink",required = false) String contentlink,
                                      @RequestParam(value = "websiteId",required = false) String websiteId){
        OfficialContent officialContent=new OfficialContent();
        officialContent.setWebsiteId(Integer.parseInt(websiteId));
        officialContent.setContentlink(contentlink);
        officialContent.setContent(content);
        if (officialWebsiteService.addContent(officialContent)){
            return "redirect:/official/Brand_Manage.html";
        }
        return "Add_Brand_Manage";
    }

    /**
     * 添加数据 1-1
     * @return
     */
    @RequestMapping("/product-category-add.html")
    public String picture_add2(){
        return "product-category-add";
    }
}
